package com.travelsky.airchina.boarding.dao.impl.hibernate;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.travelsky.airchina.boarding.dao.CategoryAttributeDAO;
import com.travelsky.airchina.boarding.domain.CategoryAttribute;

@Repository("categoryAttributeDAO")
public class CategoryAttributeDAOImpl extends GenericDAOHibernate<CategoryAttribute, Long,CategoryAttributeDAO> implements CategoryAttributeDAO{
	@Autowired
	public CategoryAttributeDAOImpl(SessionFactory sessionFactory){
		this.setSessionFactory(sessionFactory);
	}
	@SuppressWarnings("unchecked")
	public List<CategoryAttribute> findCategoryAttributeListByCategoryId(Long categoryId) {
		String hql = "from CategoryAttribute where category.id = ? ";
		return this.getHibernateTemplate().find(hql, categoryId);
	}

	@SuppressWarnings("unchecked")
	public List<CategoryAttribute> findCategoryAttributeListByCategoryId(Long categoryId, String status) {
		String hql = "from CategoryAttribute where category.id = ? and status = ? ";
		return this.getHibernateTemplate().find(hql, new Object[]{categoryId,status});
	}

	public void deleteCategoryAttributeCandidateValueByCategoryAttributeId(Long id) {
		String hql = "delete from CategoryAttributeCandidateValue where categoryAttribute.id = ? ";
		this.getHibernateTemplate().bulkUpdate(hql, id);
		
	}

	public void updateCategoryAttributeStatus(Long id, String status) {
		String hql = "update CategoryAttribute set status = ? where id = ?";
		this.getHibernateTemplate().bulkUpdate(hql, new Object[]{status,id});
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> findAttributeIdAndNames(List<Long> attributeIds) {
		String hql = " select attribute_.id,attribute_.name from CategoryAttribute attribute_ where attribute_.id in (:attributeIds)";
		Query query = this.getSession().createQuery(hql);
		query.setParameterList("attributeIds", attributeIds,Hibernate.LONG);
		return query.list();
	}

	@SuppressWarnings("unchecked")
	public List<CategoryAttribute> findCategoryAttributeListByCategoryIds(List<Long> categoryIds) {
		String hql = " from CategoryAttribute attribute_ where attribute_.category.id in (:categoryIds)";
		Query query = this.getSession().createQuery(hql);
		query.setParameterList("categoryIds", categoryIds,Hibernate.LONG);
		return query.list();
	}

}
